package com.bsj.sipgateway.sip.transmit.request.impl;

import com.bsj.power.common.def.entity.gb28181.Device;
import com.bsj.sipgateway.cache.DeviceCache;
import com.bsj.sipgateway.command.RocketMqCommand;
import com.bsj.sipgateway.common.constant.Constants;
import com.bsj.sipgateway.mq.producer.CommandUpProducer;
import com.bsj.sipgateway.sip.bean.Command;
import com.bsj.sipgateway.sip.bean.TempData;
import com.bsj.sipgateway.sip.bean.VideoStreamSession;
import com.bsj.sipgateway.sip.session.CallIdManager;
import com.bsj.sipgateway.sip.session.CommandManager;
import com.bsj.sipgateway.sip.session.VideoStreamSessionManager;
import com.bsj.sipgateway.sip.transmit.request.SIPRequestAbstractProcessor;
import lombok.extern.slf4j.Slf4j;

import javax.sip.RequestEvent;
import javax.sip.ServerTransaction;
import javax.sip.header.CallIdHeader;
import javax.sip.message.Request;

/**
 * @author bsj-chenjunkang
 * @Description:ACK请求处理器
 * @date 2022/8/31
 */
@Slf4j
public class AckRequestProcessor extends SIPRequestAbstractProcessor {

    private CommandUpProducer commandUpProducer;

    private VideoStreamSessionManager sessionManager;

    /**
     * 处理  ACK请求
     *
     * @param evt
     */
    @Override
    public void process(RequestEvent evt) {
        try {
            //处理语音广播的ACK
            Request request = evt.getRequest();
            //根据callid找到缓存的数据（设备id,设备ip,设备端口等）
            CallIdHeader callIdHeader = (CallIdHeader) request.getHeader(CallIdHeader.NAME);
            TempData tempData = CallIdManager.get(callIdHeader.getCallId());
            if (tempData == null) {
                log.info("ACK请求没有找到缓存数据，callId:{}", callIdHeader.getCallId());
                return;
            }
            String deviceId = tempData.getDeviceId();
            Device device = DeviceCache.getDevice(deviceId);
            Long did = device.getId();

            Command cache = CommandManager.getWithBroadcast(deviceId);
            log.info("接收到语音广播ack，cache={}", cache.toString());
            if (cache != null) {
                ServerTransaction serverTransaction = getServerTransaction(evt);
                //缓存信息，后续bye使用
                VideoStreamSession session = sessionManager.putIfAbsent(deviceId,
                        Constants.BROADCAST_PREFIX, serverTransaction.getDialog(), null, null, cache.getChannel());
                if (session != null) {
                    //这种情况基本不会出现
                    log.error("ACK请求时，设备已经有语音广播，VideoStreamSession不为空");
                    return;
                }

                //响应mq
                RocketMqCommand command = new RocketMqCommand(cache.getCmdType(), cache.getUuid(), deviceId, 1);
                commandUpProducer.send(command, Constants.SQL_UPDATE);
                CommandManager.removeWithBroadcast(deviceId);
            } else {
                log.info("ACK请求没有找到mq命令，deviceId:{}", deviceId);
            }
        } catch (Exception e) {
            log.error("处理ACK请求异常", e);
        }
    }

    public void setCommandUpProducer(CommandUpProducer commandUpProducer) {
        this.commandUpProducer = commandUpProducer;
    }

    public void setSessionManager(VideoStreamSessionManager sessionManager) {
        this.sessionManager = sessionManager;
    }

}
